// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html
var game_scene = cc.require("game_scene");
cc.Class({
    extends: cc.Component,

    properties: {
        init_speed:150,
        power:600,
        y_ratio:0.5560472,
        direct:1,
        leftPoint:180,
        rightPoint:580,
        mapManager:{
            type:game_scene,
            default:null
        },
        nextBlock:{
            default:null,
            type:cc.Node
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.direct = 1;
    },

    start () {
        // 旋转单位
        this.rotate_node = this.node.getChildByName("rotate");
        // 压缩单位
        this.anim_node = this.rotate_node.getChildByName("anim");
        this.isReadyJump = false;

        // 按下屏幕
        this.anim_node.on(cc.Node.EventType.TOUCH_START, function(){
            this.speed = this.init_speed;
            this.x_distance = 0;
            this.y_distance = 0
            this.isReadyJump = true;
            // 停止所有动作
            this.anim_node.stopAllActions();
            // 进行缩放
            this.anim_node.runAction(cc.scaleTo(2,1,0.5));
        }.bind(this), this);

        // 离开屏幕
        this.anim_node.on(cc.Node.EventType.TOUCH_END, function(){
            this.isReadyJump = false;
            // 停止所有动作
            this.anim_node.stopAllActions();
            // 缩放为正常大小
            this.anim_node.runAction(cc.scaleTo(0.5,1,1));
            // 跳跃函数
            this.jumpNow();
        }.bind(this), this);

        // 取消
        this.anim_node.on(cc.Node.EventType.TOUCH_CANCEL, function(){
            this.isReadyJump = false;
            this.isReadyJump = false;
            this.anim_node.stopAllActions();
            this.anim_node.runAction(cc.scaleTo(0.5,1,1));
        }.bind(this), this);
    },

    jumpNow () {
        // x轴移动距离
        var x_distance = this.x_distance;
        // y轴移动距离
        var y_distance = x_distance * this.y_ratio;
        // 旋转360
        this.rotate_node.runAction(cc.rotateBy(0.5,360 * this.direct));
        var target_pos = this.node.getPosition();
        target_pos.x += x_distance * this.direct;
        target_pos.y += y_distance;
        //  移动视角
        var worldPos = this.node.parent.convertToWorldSpaceAR(target_pos);
        var isGameOver = false;
        if (this.nextBlock.getComponent("block").isJumpSuccess(worldPos, this.direct)) {
            target_pos = this.node.parent.convertToNodeSpaceAR(worldPos);
        } else {
            isGameOver = true;
        }
        var endAction = cc.callFunc(function(){
            if (isGameOver) {
                // GG
                this.mapManager.onGameOver();
            } else {
                // 随机生成下一次跳跃的方向
                this.direct = (Math.random() < 0.5) ? -1 : 1;
                if (this.direct === -1) {
                    // 下一次往左跳，镜头移动到右边
                    this.mapManager.moveMap(this.rightPoint-worldPos.x, -y_distance);
                } else {
                    // 下一次往右跳，镜头移动到左边
                    this.mapManager.moveMap(this.leftPoint-worldPos.x, -y_distance);
                }
            }
        }.bind(this));
        // 跳跃
        var jumpAction = cc.jumpTo(0.5, target_pos, 200, 1);
        // 先跳跃，后移动镜头
        var seq = cc.sequence(jumpAction, endAction);
        this.node.runAction(seq);
    },

    setNextBlock (block) {
        this.nextBlock = block;
    },

    update (dt) {
        if (this.isReadyJump) {
            this.speed += this.power * dt;
            this.x_distance += this.speed * dt;
        }
    },
});
